import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

interface GlobalState {
  votes: number
  addVotes: () => void
  subtractVotes: () => void
  fetchData: () => void
}

export const useStore = create<GlobalState>()(
  persist(
    (set) => ({
      votes: 0,
      addVotes: () => set((state) => ({ votes: state.votes + 1 })),
      subtractVotes: () => set((state) => ({ votes: state.votes - 1 })),
      fetchData: async () => {
        await new Promise((resolve) => setTimeout(resolve, 1000))
        set({ votes: 1000 })
      }
    }),
    { name: 'basket', storage: createJSONStorage(() => localStorage) }
  )
)
